//
//  ModifyUserInfoViewController.m
//  PigMarket
//
//  Created by Felix on 2019/3/15.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "ModifyUserInfoViewController.h"

#import "ModifyNameView.h"
#import "SetupPasswordView.h"
#import "ModifyPersonalRemarkView.h"

#import "JTextView.h"

@interface ModifyUserInfoViewController ()

/**
 modifyNameView
 */
@property (nonatomic , strong) ModifyNameView *modifyNameView;
/**
 setupPasswordView
 */
@property (nonatomic , strong) SetupPasswordView *setupPasswordView;
/**
 modifyPersonalRemarkView
 */

@property (nonatomic , strong) JTextView *modifyPersonalRemarkView;


@end

@implementation ModifyUserInfoViewController

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initAction];
    self.baseNavView.navTitleLabel.text = self.title;

}


- (void)initView {
    switch (self.modifyUserInfoType) {
        case ModifyUserInfoTypeName:
            [self.view addSubview:self.modifyNameView];
            [self.modifyNameView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(TJ_NavBarHeight);
            }];
            break;
        case ModifyUserInfoTypePassword:
            [self.view addSubview:self.setupPasswordView];
            [self.setupPasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(TJ_NavBarHeight);

            }];
            break;
        case ModifyUserInfoTypePersonalRemark:
            
            [self.view addSubview:self.modifyPersonalRemarkView];
            [self.modifyPersonalRemarkView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(TJ_NavBarHeight);

            }];
            
            break;
    }
}


- (void) initAction {
    
    switch (self.modifyUserInfoType) {
        case ModifyUserInfoTypeName:
            [self.modifyNameView.saveBtn addTarget:self action:@selector(modifyNameSaveTapped) forControlEvents:UIControlEventTouchUpInside];
            break;
        case ModifyUserInfoTypePassword:
            [self.setupPasswordView.saveBtn addTarget:self action:@selector(setupPasswordSaveTapped) forControlEvents:UIControlEventTouchUpInside];
            break;
        case ModifyUserInfoTypePersonalRemark:
            [self.modifyPersonalRemarkView.confirmBtn addTarget:self action:@selector(modifyPersonalRemarkViewSaveTapped) forControlEvents:UIControlEventTouchUpInside];

            break;
    }
}


#pragma mark - delegate

#pragma mark - target action
- (void)modifyNameSaveTapped {
    NSString *nickName = self.modifyNameView.nicknameField.text;
    self.userInfoModel.nickName = nickName;
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationRemarkChanged object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupPasswordSaveTapped {
   
    //保存密码
    NSString *password = self.setupPasswordView.passwordField.text;
    NSString *confirmPassword = self.setupPasswordView.confirmPasswordField.text;
    if (![password isEqualToString:confirmPassword]) {
        [self toastWithContent:@"两次密码输入不一致"];
        return;
    }
    self.userInfoModel.password = password;
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationPasswordChanged object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)modifyPersonalRemarkViewSaveTapped {
    //保存个性签名
    self.userInfoModel.personality = self.modifyPersonalRemarkView.remarkTextView.text;
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationRemarkChanged object:nil];

    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - private method

#pragma mark - getter and setter
- (void)setModifyUserInfoType:(ModifyUserInfoType)modifyUserInfoType {
    _modifyUserInfoType = modifyUserInfoType;
    
    [self setUpView];
}

- (ModifyNameView *)modifyNameView {
    if (!_modifyNameView) {
        _modifyNameView = [[ModifyNameView alloc] init];
    }
    return _modifyNameView;
}
- (SetupPasswordView *)setupPasswordView {
    if (!_setupPasswordView) {
        _setupPasswordView = [[SetupPasswordView alloc] init];
    }
    return _setupPasswordView;
}

- (JTextView *)modifyPersonalRemarkView {
    if (!_modifyPersonalRemarkView) {
        _modifyPersonalRemarkView = [[JTextView alloc] initWithTextViewType:TextViewTypePersonalRemark title:@"个性签名" placeholderLabStr:@"请输入个性签名" limitMaxWord:100 confirmBtnTitle:@"保存"];
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return _modifyPersonalRemarkView;
}
@end
